<template>
  <!--选择受托方-->
  <section class="component select-shou-tuo-fang">
    <data-box :data="dataSet" :selectionList.sync="selectionList">
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column prop="outsName" label="受托方" :min-width="$helper.getColumnWidth(3)"></el-table-column>
      </template>
    </data-box>
    <div class="operate-buttons">
      <el-button @click="cancel" >取消</el-button>
      <el-button @click="submit" :disabled="selectionList.length === 0">确定</el-button>
    </div>
  </section>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import DataBox from "~/components/common/data-box.vue";
import { Emit, Prop } from "vue-property-decorator";
import { PageService } from "~/utils/page.service";
import { Dependencies } from "~/core/decorator";
import { OutsourceService } from "~/services/domain-service/outsource.service";
import { OutsourceCollectionService } from "~/services/domain-service/outsource-collection-manage.service";


@Component({
  components: {
    DataBox
  }
})
export default class SelectShouTuoFang extends Vue {
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(OutsourceService)
  private outsourceService: OutsourceService;
  @Dependencies(OutsourceCollectionService)
  private outsourceCollectionService: OutsourceCollectionService;
  private selectionList: any[] = []; //选中的数据
  private dataSet: any[] = []; //受托方列表数据
  private params:any = {
      caseIds:[],
      outsourceIds:[]
    }


  @Prop({
    required: true,
    default: () => []
  })
  private selectedCaseIds: any[]

  /**
   * 是否全部数据
   */
  @Prop({ required: true })
  private isLawsuit: boolean;

  @Emit("close")
  private emitClose() {
    this.selectionList = [];
    this.refresh();
  }

  @Emit('success')
  private emitSuccess() { }

  private refresh() {
    // 默认加载列表
    this.outsourceService
      .getOutSourceList()
      .subscribe(data => {
        this.dataSet = data;
      });
  }

  private mounted() {
    this.refresh();
  }

  private get commissionIds() {
    return this.selectionList.map(v => v.id)
  }
  private onClose() {
    this.emitClose()
  }
  private submit(){
    this.params.caseIds = this.selectedCaseIds
    this.params.outsourceIds = this.commissionIds
    this.outsourceCollectionService
      .repeatDistributeOutcase(this.params)
      .subscribe(data => {
        this.$message("分配成功")
        this.onSuccess()
      });
  }
  private cancel(){
    this.emitClose()
  }
  private onSuccess() {
    this.emitClose()
    // 提示父页面进行数据刷新
    this.emitSuccess()
  }

  private onOpen() {
    this.$nextTick(() => {
      const component = this.$refs.distribution as any
      component.refresh()
    })
  }
}
</script>
<style lang="less" scoped>
</style>
